package com.jzlin.funhub.admin.service.question;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzlin.funhub.admin.mapper.QuestionAnswerMapper;
import com.jzlin.funhub.admin.model.dto.QuestionAnswerPageDTO;
import com.jzlin.funhub.admin.model.dto.QuestionPageDTO;
import com.jzlin.funhub.admin.model.entity.QuestionAnswer;
import com.jzlin.funhub.admin.model.req.QuestionAnswerPageReq;
import com.jzlin.funhub.admin.model.req.QuestionAnswerPublishReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 问题回答表 服务实现类
 * </p>
 *
 * @author jzlin
 * @since 2025-07-07
 */
@Service
public class QuestionAnswerService extends ServiceImpl<QuestionAnswerMapper, QuestionAnswer> {

    @Autowired
    private QuestionService questionService;

    /**
     * 分页查询列表
     *
     * @param req 查询条件
     * @return
     */
    public IPage<QuestionAnswerPageDTO> queryPage(QuestionAnswerPageReq req) {
        Page<QuestionAnswer> page = new Page<>(req.getCurrent(), req.getSize());
        return this.baseMapper.queryPage(page, req);
    }

    /**
     * 发布回答
     * @param req
     */
    @Transactional
    public void publishNewAnswer(QuestionAnswerPublishReq req) {
        QuestionAnswer questionAnswer = new QuestionAnswer();
        questionAnswer.setQuestionId(req.getQuestionId());
        questionAnswer.setContent(req.getContent());

        this.save(questionAnswer);
        questionService.increaseAnswerCount(req.getQuestionId());
    }
}
